package com.example.waimai.ui.dashboard;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.waimai.HttpUtil.MyPost;
import com.example.waimai.context.UserInfoContext;
import com.example.waimai.pojo.Orders;
import com.example.waimai.util.MyThreadPool;
import com.example.waimaiandroid.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DashboardFragment extends Fragment {


    private  View view ;
    private static List<Orders> data = new ArrayList<>();
    private static ListView ordersListView;
    private orderItemDashAdapter orderItemNotiAdapter;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what){
                case 1:
                    // 通知更新
                    if(orderItemNotiAdapter != null){
                        orderItemNotiAdapter.updateAll(data);
                    }
                    break;
            }
        }
    };

    public static void setPinjiaButton(int position){
        orderItemDashAdapter.ViewHolder tag = (orderItemDashAdapter.ViewHolder) ordersListView.getChildAt(position).getTag();
        // 更新一下数据呗
        data.get(position).setUserStatus(2);
        tag.noti_pinjia.setVisibility(View.GONE);

    }

    @Override
    public void onAttach(@NonNull Context context) {
        super.onAttach(context);
        initData();
    }

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.fragment_dashboard, null, false);
        setHasOptionsMenu(true);
        // 获取数据
        ordersListView = view.findViewById(R.id.in_orders);
        orderItemNotiAdapter = new orderItemDashAdapter(data,this.getContext());
        ordersListView.setAdapter(orderItemNotiAdapter);
        //initData();
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
       // initData();
    }

    // 菜单
    @Override
    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.refresh_menu,menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        initData();
        return super.onOptionsItemSelected(item);
    }

    private void initData(){
        // 获取到订单数据 根据 storeId 和  Storestatus为0
        MyThreadPool.doJob(new Runnable() {
            @Override
            public void run() {
                String url = "getUserOrderByUserId";
               // 这边的 订单 包含了店铺和地址以及收货地址信息
                data = (List<Orders>) MyPost.doPost(url, UserInfoContext.mUser.getId());
                Message msg = new Message();
                msg.what = 1;
                handler.sendMessage(msg);
            }
        });
    }
}